﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using System.Web.Security;
using ZLCMS.Business.Commands.SessionCommands;
using DotNet.Common.Command;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using NHibernate.Criterion;

namespace ZLCMS.Web.MemberUser
{
        public partial class BindAccount : System.Web.UI.Page
        {
                protected void Page_Load(object sender, EventArgs e)
                {
                        if(!IsPostBack)
                        {
                                hid_oid.Value = Request["openid"];
                                hid_type.Value=Request["type"];
                        }
                }

                protected void btnSubmit_Click(object sender, EventArgs e)
                {
                        ClientScriptManager csm = this.Page.ClientScript;

                        MemberInfo member;
                        String md5Code = FormsAuthentication.HashPasswordForStoringInConfigFile(tb_pw.Text.Trim(), "MD5").ToLower();
                        using (ISession session = SessionFactoryHelper.CreateSession())
                        {
                                member = session.CreateQuery("from MemberInfo where Password=:pwd  and ( MemberName=:id or Telephone=:id or Email=:id )").SetParameter("id", tb_id.Text).SetParameter("pwd", md5Code).UniqueResult<MemberInfo>();

                                if (member == null)
                                {
                                        csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('账户无效。');", true);
                                }
                                else
                                {
                                        if (hid_type.Value == "weibo")
                                        {
                                                member.SinaOpenId = hid_oid.Value;
                                        }
                                        else
                                        {
                                                member.QqOpenId = hid_oid.Value;
                                        }
                                        session.Update(member);
                                        session.Flush();
                                        csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('操作成功。');window.location.href='/MemberUser/Default.aspx'", true);
                                        //登录
                                        ICommandResult<LoginResult> res = new LoginCommand().Execute(new LoginParameter() { Telephone = tb_id.Text.Trim(), Password = md5Code });

                                        if (!res.Error)
                                        {
                                                //转发到请求的页面
                                                Response.Redirect("/MemberUser/Default.aspx");
                                        }
                                }
                        }
                }
        }
}
